<?php


namespace App\Process;

use EasySwoole\Utility\File;
use EasySwoole\HttpClient\HttpClient;

class ImgDown extends SpiderBase
{

    /**
     * @inheritDoc
     */
    function handelProcess($data)
    {
        //获取url最后的后缀
        $pathInfo   = pathinfo($data['url']);
        $file       = EASYSWOOLE_ROOT . "/Down/{$data['title']}/" . "{$data['alt']}.{$pathInfo['extension']}";
        $httpClient = new HttpClient($data['url']);
        // 创建一个空文件
        File::createFile($file, '');
        // 下载
        $result = $httpClient->download($file);
        return $result;
    }

    function beforeHandel($data)
    {
        // TODO: Implement beforeHandel() method.
    }

    function afterHandel($result, $data)
    {
        if ($result === false) {
            $this->console("下载错误");
            $this->tryAgain($data);
            return;
        }
    }
}